#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"
#include <math.h>
#include <stdlib.h>
#include <time.h>

#define NOISY_TEST 1

int testNum= 1;

int test()
{
        struct gameState state;
        struct gameState * game = &state;
        int r;

        //initialize game

        int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse,sea_hag, tribute, smithy};
        int numPlayers = 2 + rand()%3;
        memset(&state, 23, sizeof(struct gameState));
        r = initializeGame(numPlayers, k, time(NULL), game);
        int drawntreasure = 0;
        int cardDrawn = rand()%26;
        int z = 0;
        int i = rand() - rand();


        //randomize game state
        int handCount = rand()% MAX_HAND;
        int deckCount = rand()%(MAX_DECK - handCount);
        int discardCount = MAX_DECK - deckCount;
        int currentPlayer = rand()%numPlayers;
        int numBuys = rand() % 5;
        int handPos = rand()%(handCount + 1);


        game->deckCount[currentPlayer] = deckCount;
        game->handCount[currentPlayer] = handCount;
        game->discardCount[currentPlayer] = discardCount;
        game->handCount[!currentPlayer] = rand() %( MAX_HAND - handCount);
        int notCurrentPlayer = game->handCount[!currentPlayer];

        for (int i = 0; i < deckCount; i++)
                game->deck[currentPlayer][i] = rand()%26;
        for (int j = 0; j < handCount; j++)
                game->hand[currentPlayer][j] = rand()%26;
        for (int k = 0; k < discardCount; k++)
                game->discard[currentPlayer][k] = rand()%26;

        zzzconsoleroomcardplay(i, currentPlayer, handPos, game);

		if(game->handCount[currentPlayer] == handCount + 3 && game->numBuys == 2 && game->handCount[!currentPlayer] == notCurrentPlayer + 1) {
#if(NOISY_TEST == 1)
                printf("Test number: %d\n", testNum);
                testNum = testNum++;
                printf("zzzconsoleroomcardplay(): PASS when test drawing consolroom card\n");
                printf("game->numbuys should be 2. It is: %d\n", game->numBuys);
                printf("game->handcount[!currentPlayer] should be : %d.  It is :%d \n", notCurrentPlayer + 1, game->handCount[!currentPlayer]);
                printf("number players = %d\ncurrent player = %d\nhandcount= %d\n", numPlayers, currentPlayer, handCount);
#endif
                                }
        else {
#if(NOISY_TEST == 1)
                printf("Test number: %d\n", testNum);
                testNum = testNum++;
                printf("zzzconsolroomcardplay(): FAIL when test drawing consolroom card\n");
                printf("game->handcount[!currentPlayer] should be : %d.  It is :%d \n", notCurrentPlayer + 1, game->handCount[!currentPlayer]);
                printf("game->numbuys should be 2. It is: %d\n", game->numBuys);
                printf("handCount: %d\n", handCount);
#endif
                return -1;
        }

        return 0;
}

int main(int argc, char *argv[])
{
        srand(time(NULL));

        for (int i = 0; i < 15000; i++) {
                if (test() == -1)
                        break;
        }

        return 0;
}

